<?php
/**
 * Created by PhpStorm.
 * User: zhao
 * Date: 2018/5/11
 * Time: 16:45
 */

namespace App\Repositories;


use App\Models\MemberOptionalField;
use App\Models\UserMcardRecord;
use App\Models\WxuserMcard;

class BaseRepository
{
    /**
     * @return 登录用户信息
     */
    public function getSeesionUser()
    {
        return session('web_auth_user');
    }

    /**
     * @return 登录会员卡信息
     */
    public function getSeesionWxuseMcard()
    {
        return session('web_wxuser_mcard');
    }

    /**
     * @return WxuserMcard model list
     */
    public function getCardsByAuthUser()
    {
        $id = $this->getSeesionUser()->primaryKey;
        return UserMcardRecord::where('table_type',$this->getSeesionUser()->table)
            ->where('table_id',$this->getSeesionUser()->$id)
            ->get();
    }

    public function getRedirectUrl()
    {
        $cards = $this->getCardsByAuthUser();

        if($cards->count()>1){

            return [
                'route'=>route('uCardList'),
                'data'=>'',
            ];

        }elseif ($cards->count()==1){

            session(['web_wxuser_mcard'=>WxuserMcard::find($cards->first()->id_wxuser_mcard)]);

            return [
                'route'=>null,
                'data'=>''
            ];
        }

        return [
            'route'=>route('bindPhone'),
            'data'=>''
        ];
    }
}